import * as THREE from 'three'
import { GangTi } from "./GangTi.js"
import { huanJing } from '../../huanJing/HuanJing.js'
import { benDiXiangLiangZhuanShiJie } from '../../gongJu/gongJu.js'

// 行星
export class XingXing extends GangTi {
	shiXingXing = true // 是行星
	gongZhuanBanJing = 0 // 公转半径
	gongZhuanZhouQi = 0 // 公转周期，单位秒
	ziZhuanZhouQi = 0 // 自转周期，单位秒
	xianSuDu = null // 星球的线速度
	zhongLiJiaSuDu = 9.8 // 重力加速度
	
	constructor(sphere, pengZhuang, yinLiJuLi = 700, jiaZaiJuLi = 1e+5, gongZhuanBanJing = 7000, gongZhuanZhouQi = 600, ziZhuanZhouQi = 60 ) {
		super(sphere, pengZhuang, 1e+6)
		
		this.yinLiJuLi = yinLiJuLi
		this.jiaZaiJuLi = jiaZaiJuLi
		this.gongZhuanBanJing = gongZhuanBanJing // 暂时无用
		this.gongZhuanZhouQi = gongZhuanZhouQi
		this.ziZhuanZhouQi = ziZhuanZhouQi
	}
	
	// 公转
	gongZhuan(jianGeShi) {
		if (!this.gongZhuanZhouQi) return
		let gongZhuanHuDu = 2 * Math.PI / this.gongZhuanZhouQi * jianGeShi
		let weiZhi = this.moXing.position.clone()
		let gongZhuanZhou = new THREE.Vector3(0, 1, 0) // 公转轴
		weiZhi.applyAxisAngle(gongZhuanZhou, gongZhuanHuDu) // 公转下个坐标位置

		let suDuFangXiang = new THREE.Vector3().subVectors(weiZhi, this.moXing.position) // 局部坐标系两个位置的向量（弦长）
		this.fu.moXing.updateWorldMatrix(true)
		benDiXiangLiangZhuanShiJie(suDuFangXiang, this.fu.moXing.matrixWorld) // 转为世界向量
		if (jianGeShi == 0) return
		let xianSuDu = suDuFangXiang.divideScalar(jianGeShi)
		if (this.fu.xianSuDu) xianSuDu.add(this.fu.xianSuDu) // 加上父行星的线速度

		this.sheZhiPengZhuangTi({ xianSuDu })
		this.xianSuDu = xianSuDu
	}
	
	// 自传
	ziZhuan(jianGeShi) {
		if (!this.ziZhuanZhouQi) return
		let ziZhuanHuDu = 2 * Math.PI / this.ziZhuanZhouQi
		let suDuFangXiang = new THREE.Vector3(0, 1, 0)
		benDiXiangLiangZhuanShiJie(suDuFangXiang, this.fu.moXing.matrixWorld)
		let jiaoSuDu = suDuFangXiang.multiplyScalar(ziZhuanHuDu)
		this.sheZhiPengZhuangTi({ jiaoSuDu })
	}
	
	gengXin(jianGeShi) {
		super.gengXin(jianGeShi)
		this.gongZhuan(jianGeShi)
		this.ziZhuan(jianGeShi)
	}
}